classdef NMRGJob < MeasurementComponent.Jobs.MeasurementJob
    properties

    end
    
    methods
        function obj = NMRGJob(nmrg, jobname)
            p=inputParser;
            p.addRequired('nmrg', @(x) isa(x, 'Table1.NMRGyroscope'));
            p.addRequired('jobname', @ischar);
            p.parse(nmrg, jobname);
            
            obj@MeasurementComponent.Jobs.MeasurementJob(p.Results.nmrg, p.Results.jobname);
            
            obj.dataPackage.parameters = struct();
            obj.dataPackage.measurement_data = struct();
            obj.dataPackage.analysis_result = struct();
            obj.dataPackage.summary = struct();
        end
        
        function disp(obj)
            disp(struct2table(obj.dataPackage.parameters));
            disp(struct2table(obj.dataPackage.measurement_data));
            disp(struct2table(obj.dataPackage.analysis_result, 'AsArray', true));
            disp(struct2table(obj.dataPackage.summary));
        end
    end
    
    %%
    methods(Static)
        function res = compare(objList, varargin)
            p = inputParser;
            p.addParameter('isPlot', true, @islogical);
            p.addParameter('viewer', [], @(x)  isa(x, 'Table1.GUI.DataViewer') || isempty(x));
            p.parse(varargin{:});
            
            res.param        = arrayfun(@(obj) obj.dataPackage.parameters, objList);
            res.measuredData = arrayfun(@(obj) obj.dataPackage.measurement_data, objList);
            res.analysis     = arrayfun(@(obj) obj.dataPackage.analysis_result, objList);
            res.summary      = arrayfun(@(obj) obj.dataPackage.summary, objList);
            
            if ~isempty(p.Results.viewer)
                Table1.NMRGJobs.NMRGJob.updateViewer(p.Results.viewer, res.param, res.summary)
            end
        end
        
        function updateViewer(viewer, param, summary)
            viewer.gui.uiHandle.tableParam.Visible = 'off';
            viewer.gui.uiHandle.tableRes.Visible = 'off';

            if isscalar(param)
                viewer.gui.uiHandle.tableParam.Data = struct2cell( param );
                viewer.gui.uiHandle.tableParam.RowName = fieldnames( param );
                viewer.gui.uiHandle.tableParam.ColumnName = {'Values'};
                
                viewer.gui.uiHandle.tableRes.Data = struct2cell( summary );
                viewer.gui.uiHandle.tableRes.RowName = fieldnames( summary );
                viewer.gui.uiHandle.tableRes.ColumnName = {'Values'};
            else
                startTime = arrayfun(@(t) t.start, summary, 'UniformOutput', false);
                
                viewer.gui.uiHandle.tableParam.Data = struct2table(param);
                viewer.gui.uiHandle.tableParam.RowName = startTime;
                viewer.gui.uiHandle.tableParam.ColumnName =  fieldnames(param);
                
                viewer.gui.uiHandle.tableRes.Data = struct2table(summary);
                viewer.gui.uiHandle.tableRes.RowName = startTime;
                viewer.gui.uiHandle.tableRes.ColumnName = fieldnames(summary);
            end

            s = uistyle; s.HorizontalAlignment = 'center';
            s = uistyle; s.HorizontalAlignment = 'center';
            viewer.gui.uiHandle.tableParam.addStyle(s);
            viewer.gui.uiHandle.tableRes.addStyle(s);
            viewer.gui.uiHandle.tableParam.Visible = 'on';            
            viewer.gui.uiHandle.tableRes.Visible = 'on';
        end
    end
end

